package com.eascs.admin.o2o.compensatory.dao;

import org.springframework.stereotype.Repository;

import com.eascs.admin.common.dao.BaseO2oDao;

@Repository
public class GeneratedContainerNumberDao extends BaseO2oDao {

	//生成柜号
	public String getContainerNumber(){
		String number = "";
		//判断表中是否有数据
		Boolean tableISNull = TableISNull();
		if( tableISNull ){
			number = "1";
		}else{
			//获取最大的柜号
			Integer maxNumber = MaxNumber();
			//判断最大的柜号个数（小于500未true，大于或等于500未false）
			Boolean maxNumberCount = MaxNumberCount(maxNumber);
			if( maxNumberCount ){
				//如果最大柜号个数小于500，柜号等于最大柜号
				number = maxNumber.toString();
			}else{
				//如果最大柜号大于或等于500，柜号加1
				Integer numbers = maxNumber + 1;
				number = numbers.toString();
			}
		}
		
		return number;
	}
	
	//判断表中是否有数据
	public Boolean TableISNull(){
		Boolean flag = true;
		String sql = "SELECT COUNT(1) FROM compensatory_data_enter";
		int num = this.findInteger(sql);
		if( num > 0 ){
			flag = false;
		}
		return flag;
	}
	
	//获取数据库中最大的柜号
	public Integer MaxNumber(){
		String sql = "SELECT c.cabinet_no FROM compensatory_data_enter c GROUP BY c.cabinet_no ORDER BY c.cabinet_no DESC LIMIT 0, 1";
		return this.findInteger(sql);
	}
	
	//判断最大柜号的个数
	public Boolean MaxNumberCount(Integer maxNumber){
		Boolean flag = false;
		String sql = "SELECT COUNT(1) FROM compensatory_data_enter c WHERE c.cabinet_no = ? ";
		int num = this.findInteger(sql, maxNumber);
		if( num < 500 ){
			flag = true;
		}
		return flag;
	}
	
	
}
